/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQml 2.12
import org.ukui.tablet 1.0
import "./baseComponent"
import "."

Rectangle {
    id: container
    property string containerTitle: qsTr("container")
    property var model: null
    property int groupId: -1
    property bool titleEditable: false
    property bool titleFocus: false

    signal itemStartDrag(bool start)
    signal showAddAppWindow(int groupId, int pageIndex)
    signal clicked()
    signal focusChange()

    radius: 12
    color: Qt.rgba(1, 1, 1, 0.75)
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 0
        anchors.topMargin: 24 * Style.heightScaleFactor
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 0
        TitleEdit {
            id: titleText
            title: containerTitle
            readOnly: !titleEditable
            Layout.minimumHeight: Style.heightScaleFactor * 32
            Layout.maximumHeight: Style.heightScaleFactor * 32
            Layout.alignment: Qt.AlignHCenter
            color: titleEditable ? "white" : "transparent"
            onContainerTitleChange: {
                modelManager.setGroupName(newTitle, groupId)
            }
            Component.onCompleted: {
                Layout.preferredWidth = Qt.binding(function() {
                    return titleEditable ?  titleText.txtwidth : 160 * Style.widthScaleFactor
                })
            }
        }
        EduLauncherItemView {
            id: itemView                        
            Layout.fillWidth: true
            Layout.preferredWidth: container.width
            Layout.fillHeight: true
            Layout.alignment: Qt.AlignHCenter
            viewMoel: model
            groupId: container.groupId
            opacity: 1
        }

        PageSwitchIndicator {
            id: pageIndicator
            visible: itemView.listViewItemCount() > 1
            Layout.fillWidth: true
            Layout.preferredHeight: Style.heightScaleFactor * 30
            Layout.minimumHeight: Style.heightScaleFactor * 30
            Layout.maximumHeight: Style.heightScaleFactor * 30
            Layout.alignment: Qt.AlignCenter
            pageContainer: itemView.pageContainer
        }
    }
    Component.onCompleted: {
        if (model === null) {
            console.warn("AppContainr: The model is null, please assign a model!")
        }
        itemView.startDrag.connect(container.itemStartDrag)
        itemView.showAddAppWindow.connect(container.showAddAppWindow)
        itemView.focusStateChanged.connect(container.editFocusChange)

        container.clicked.connect(itemView.containerClicked)
        container.focusChange.connect(titleText.focusHandle)

        titleText.editVisible.connect(container.editStated)
        titleText.editInVisible.connect(container.noEditStated)

    }

    MouseArea {
        z: -1
        anchors.fill: parent
        onClicked: {
            editFocusChange()
            container.clicked()
        }
    }

    function enableItemDrag(enable) {
        itemView.dragEnable = !enable
    }

    function editFocusChange(){
        container.focusChange()
    }

    function editStated(){
        titleText.Layout.preferredWidth = Qt.binding(function() {
            return Style.widthScaleFactor * 400
        })
    }

    function noEditStated(){
        titleText.Layout.preferredWidth = Qt.binding(function() {
            return titleText.txtwidth
        })
    }
}
